﻿using System.Text.Encodings.Web;
using System.Text.Json;

namespace DotNetHackathon.GBA.ApiCommon.JsonSerializing
{
    public class DefaultJsonSerializer : IJsonSerializer
    {
        private static readonly JsonSerializerOptions _jsonSerializerOptions = new()
        {
            AllowTrailingCommas = true,
            //IgnoreNullValues = true,
            IgnoreReadOnlyProperties = false,
            MaxDepth = 8,
            PropertyNameCaseInsensitive = false,
            PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
            ReadCommentHandling = JsonCommentHandling.Disallow,
            WriteIndented = false,
            Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
            DefaultBufferSize = 1024
        };

        public T Deserialize<T>(string jsonString)
        {
            if (jsonString != null)
            {
                return JsonSerializer.Deserialize<T?>(jsonString, _jsonSerializerOptions);
            }

            return default(T);
        }

        public string Serialize(object obj)
        {
            return JsonSerializer.Serialize(obj, _jsonSerializerOptions);
        }
    }
}
